﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Xsl;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using MPMovieCatalog.Domain;
using MPMovieCatalog.Service;
using System.ComponentModel;


namespace MPMovieCatalog
{
    public partial class ApplicationForm : Form
    {
        private IMovieService movieService = null;

        public ApplicationForm()
        {
            InitializeComponent();
        }

        private void ApplicationForm_Load(object sender, EventArgs e)
        {
            ChannelServices.RegisterChannel(new HttpClientChannel(), false);
            movieService = (IMovieService)Activator.GetObject(typeof(IMovieService)
                               , "http://localhost:8083/MovieService");

            //bindingSource.DataSource = new BindingList<MovieShortInfo>(MovieDAO.FindAllMoviesShortInfo());
            BindingList<MovieShortInfo> bindingList = new BindingList<MovieShortInfo>();
            foreach (MovieShortInfo movie in movieService.FindAllMoviesShortInfo())
            {
                bindingList.Add(movie);
            }
            bindingSource.DataSource = bindingList;
            movieCountLabel.Text = "Всего фильмов: " + bindingSource.Count.ToString();
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void movieGrid_SelectionChanged(object sender, EventArgs e)
        {

            MovieShortInfo movieShortInfo = (MovieShortInfo)bindingSource.Current;
            MovieInfo movieInfo = movieService.FindMovieInfoById(movieShortInfo.Id);

            if (movieInfo != null)
            {
                using (FileStream fileStream = new FileStream(Application.StartupPath + @"\templates\_film.html", FileMode.Create))
                {
                    XslCompiledTransform xslt = new XslCompiledTransform();
                    xslt.Load(Application.StartupPath + @"\templates\view_item_details_aqua2.xsl");
                    xslt.Transform(new XmlNodeReader(movieInfo.ToXML()), new XmlTextWriter(fileStream, Encoding.UTF8));
                }

                browser.Navigate(Application.StartupPath + @"\templates\_film.html");
            }
        }

        private void movieGrid_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            MovieShortInfo movieShortInfo = (MovieShortInfo)bindingSource.Current;
            MovieInfo movieInfo = movieService.FindMovieInfoById(movieShortInfo.Id);

            using (EditForm editForm = new EditForm())
            {
                editForm.AvailableGenres =  movieService.FindAvailableGenres(movieShortInfo.Id);
                editForm.AvailableActors = movieService.FindAvailableActors(movieShortInfo.Id);
                editForm.Data = movieInfo;
                if (editForm.ShowDialog() == DialogResult.OK)
                {
                    MovieInfo updatedMovieInfo = editForm.Data;
                    movieService.UpdateMovieInfo(updatedMovieInfo);

                    if (movieInfo.Picture != updatedMovieInfo.Picture || movieInfo.Name != updatedMovieInfo.Name)
                    {
                        MediaPortal.Util.Utils.FileDelete(MediaPortal.Util.Utils.GetLargeCoverArtName(MediaPortal.Util.Thumbs.MovieTitle, movieInfo.Name));
                        MediaPortal.Util.Utils.FileDelete(MediaPortal.Util.Utils.GetCoverArtName(MediaPortal.Util.Thumbs.MovieTitle, movieInfo.Name));

                        if (updatedMovieInfo.Picture != string.Empty)
                        {
                            string largeCoverArtName = MediaPortal.Util.Utils.GetLargeCoverArtName(MediaPortal.Util.Thumbs.MovieTitle, updatedMovieInfo.Name);
                            string coverArtName = MediaPortal.Util.Utils.GetCoverArtName(MediaPortal.Util.Thumbs.MovieTitle, updatedMovieInfo.Name);

                            if (File.Exists(updatedMovieInfo.Picture))
                            {
                                if (MediaPortal.Util.Picture.CreateThumbnail(updatedMovieInfo.Picture, largeCoverArtName, (int)MediaPortal.Util.Thumbs.ThumbLargeResolution, (int)MediaPortal.Util.Thumbs.ThumbLargeResolution, 0, MediaPortal.Util.Thumbs.SpeedThumbsSmall))
                                    MediaPortal.Util.Picture.CreateThumbnail(updatedMovieInfo.Picture, coverArtName, (int)MediaPortal.Util.Thumbs.ThumbResolution, (int)MediaPortal.Util.Thumbs.ThumbResolution, 0, MediaPortal.Util.Thumbs.SpeedThumbsLarge);
                            }
                        }
                    }

                    //bindingSource.DataSource = new BindingList<MovieShortInfo>(MovieDAO.FindAllMoviesShortInfo());
                    BindingList<MovieShortInfo> bindingList = new BindingList<MovieShortInfo>();
                    foreach (MovieShortInfo movie in movieService.FindAllMoviesShortInfo())
                    {
                        bindingList.Add(movie);
                    }
                    bindingSource.DataSource = bindingList;

                }

            }
        }

    }
}
